#include "PptpRequestHandler.h"

PptpRequestHandler::PptpRequestHandler(QObject *parent)
    : QObject{parent}
{
}
QByteArray PptpRequestHandler::userLogin(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();
    QString loginInfo = parameters["loginInfo"].toString();
    QString password = parameters["password"].toString();
    int userId = userService.userLogin(loginInfo, password);
    PptpMessage pptpResponse;
    parameters = QJsonObject();
    parameters["userId"] = userId;
    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}

QByteArray PptpRequestHandler::userRegister(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();
    QString registerInfo = parameters["registerInfo"].toString();
    QString password = parameters["password"].toString();
    int userId = userService.userRegister(registerInfo, password);
    PptpMessage pptpResponse;
    parameters = QJsonObject();
    parameters["userId"] = userId;
    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::userEditUserInfo(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();
    int userId = parameters["userId"].toInt();
    QString personName = parameters["personName"].toString();
    QString sex = parameters["sex"].toString();
    QString email = parameters["email"].toString();
    QString sign = parameters["sign"].toString();
    bool isOK = userService.updateUserInfo(userId, personName, sex, email, sign);
    PptpMessage pptpResponse;
    parameters = QJsonObject();
    parameters["isOK"] = isOK;
    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::userGetUserInfo(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    int userId = parameters["userId"].toInt();

    UserInfo userInfo = userService.getUserInfo(userId);

    PptpMessage pptpResponse;
    parameters = QJsonObject();

    parameters["personName"] = userInfo.nickname;
    parameters["sex"] = userInfo.gender;
    parameters["email"] = userInfo.email;
    parameters["sign"] = userInfo.signature;

    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::userSetAvatar(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    int userId = parameters["userId"].toInt();
    QHash<QString, QByteArray> files = pptpRequest.getFiles();

    QByteArray avatar = files.value("avatar");

    bool isOK = userService.addUserImage(userId, avatar);

    PptpMessage pptpResponse;
    parameters = QJsonObject();

    parameters["isOK"] = isOK;

    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::userGetAvatar(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    int userId = parameters["userId"].toInt();

    QByteArray avatar = userService.getUserImage(userId);

    PptpMessage pptpResponse;

    pptpResponse.addFile("avatar", avatar);

    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::userAddAddress(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    int userId = parameters["userId"].toInt();
    QString label = parameters["label"].toString();
    QString name = parameters["name"].toString();
    QString phone = parameters["phone"].toString();
    QString address = parameters["address"].toString();

    bool isOK = addressBookService.addUserAddress(userId, label, name, phone, address);

    PptpMessage pptpResponse;
    parameters = QJsonObject();

    parameters["isOK"] = isOK;

    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::userGetAddressBook(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    int userId = parameters["userId"].toInt();

    QList<Address> addressBook = addressBookService.getAddressBook(QString::number(userId));

    PptpMessage pptpResponse;
    parameters = QJsonObject();

    QJsonArray addressBookArray;
    // 遍历地址簿列表
    for (const Address &address : addressBook)
    {
        QJsonObject addressObject;
        addressObject["addressId"] = address.addressId;
        addressObject["label"] = address.label;
        addressObject["name"] = address.name;
        addressObject["phone"] = address.phone;
        addressObject["detailAddress"] = address.address;
        // 将单个地址对象添加到数组中
        addressBookArray.append(addressObject);
    }
    // qDebug() << "addressBookArray: " << addressBookArray;
    parameters["addressBook"] = addressBookArray;

    // qDebug() << "userGetAddressBook parameters: " << parameters;

    pptpResponse.setParameters(parameters);

    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::userEditAddress(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    int addressId = parameters["addressId"].toInt();
    QString label = parameters["label"].toString();
    QString name = parameters["name"].toString();
    QString phone = parameters["phone"].toString();
    QString address = parameters["address"].toString();

    bool isOK = addressBookService.updateAddress(addressId, label, name, phone, address);

    PptpMessage pptpResponse;
    parameters = QJsonObject();

    parameters["isOK"] = isOK;

    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::userDeleteAddress(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();
    qDebug() << "userDeleteAddress: " << parameters;
    int addressId = parameters["addressId"].toInt();

    bool isOK = addressBookService.removeAddress(addressId);

    PptpMessage pptpResponse;
    parameters = QJsonObject();

    parameters["isOK"] = isOK;

    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::productGetProducts(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    // qDebug() << "productGetProducts parameters: " << parameters;

    QString productType = parameters["productType"].toString();
    QString productName = parameters["productName"].toString();
    QString filterCriteria = parameters["filterCriteria"].toString();
    QString sortBy = parameters["sortBy"].toString();

    QList<Product> products = productService.getProducts(productType, productName, filterCriteria, sortBy);

    PptpMessage pptpResponse;
    parameters = QJsonObject();

    QJsonArray productBookArray;
    for (const Product &product : products)
    {
        QJsonObject addressObject;
        addressObject["productId"] = product.productId;
        addressObject["productName"] = product.productName;
        addressObject["price"] = product.price;
        addressObject["stock"] = product.stock;
        addressObject["sales"] = product.sales;
        addressObject["description"] = product.description;
        productBookArray.append(addressObject);
        pptpResponse.addFile(QString::number(product.productId), product.image);
    }
    parameters["productList"] = productBookArray;

    pptpResponse.setParameters(parameters);

    // qDebug() << "productGetProducts parameters: " << parameters;

    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::orderProductBuy(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    // qDebug() << "购买请求参数：" <<parameters;

    bool isOK = orderService.buy(parameters);

    PptpMessage pptpResponse;
    parameters = QJsonObject();

    parameters["isOK"] = isOK;

    pptpResponse.setParameters(parameters);
    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::orderOrderList(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    int userId = parameters["userId"].toInt();

    parameters = QJsonObject();

    parameters = orderService.getOrders(userId);

    PptpMessage pptpResponse;

    pptpResponse.setParameters(parameters);

    // qDebug() << "获取订单列表响应参数：" << parameters;

    return pptpResponse.wrapJson();
}
QByteArray PptpRequestHandler::productGetProductsById(const PptpMessage &pptpRequest)
{
    QJsonObject parameters = pptpRequest.getParameters();

    // qDebug() << "根据ID获取商品信息请求参数" << parameters;

    int productId = parameters["product_id"].toInt();

    Product product = productService.getProductById(productId);

    PptpMessage pptpResponse;

    parameters = QJsonObject();

    parameters["product_id"] = product.productId;

    parameters["productName"] = product.productName;

    pptpResponse.setParameters(parameters);

    // qDebug() << "根据ID获取商品信息响应参数" << parameters;

    pptpResponse.addFile(QString::number(product.productId), product.image);

    return pptpResponse.wrapJson();
}
